Ext.onReady(function(){ 

Ext.QuickTips.init();
Ext.form.Field.prototype.msgTarget = 'under';

var sm = new Ext.grid.CheckboxSelectionModel();

var cm1=new Ext.grid.ColumnModel([ 
sm,
new Ext.grid.RowNumberer({header: "编号",width:40}),
{header:'债项号',dataIndex:'debtNo',hidden:true,sortable:true},
{header:'债项编号',dataIndex:'debtId',hidden:true,sortable:true},
{header:'客户名称',dataIndex:'custName',width:80,align:"center",sortable:true},
{header:'债项名称',dataIndex:'debtDesc',width:80,align:"center",sortable:true},
{header:'债项业务品种',dataIndex:'debtType',width:80,align:"center",sortable:true},
{header:'合同金额(元)',dataIndex:'debtSum',width:80,align:"center",sortable:true},
{header:'债项余额(元)',dataIndex:'debtBalance',width:80,align:"center",sortable:true},
{header:'担保类型',dataIndex:'guarantyKind',width:80,align:"center",sortable:true},
{header:'资产分类',dataIndex:'classifyResult',width:80,align:"center",sortable:true},
{header:'认定日期',dataIndex:'classifyDate',width:100,align:"center",sortable:true},
{header:'所属机构',dataIndex:'inputOrgId',width:100,align:"center",sortable:true},
]); 
var ds=new Ext.data.Store({ 
				reader:new Ext.data.JsonReader({ },[
				{name:'debtNo'},
				{name:'debtId'},
				{name:'custName'},
				{name:'debtDesc'}, 
				{name:'debtType'},
				{name:'debtSum'},
				{name:'debtBalance'},
				{name:'classifyResult'},
				{name:'guarantyKind'},
				{name:'classifyDate'},
				{name:'inputOrgId'}
				
				]) 
});

var addIndustryTypeLevelOneStore=new Ext.data.Store({ 
	proxy:new Ext.data.HttpProxy({
	url:'showIndustryTypeLevelOne.do',
	method:'GET'
	}), 
	reader:new Ext.data.JsonReader({ },[
	{name:'name'}, 
	{name:'code'}
	]) ,
	autoLoad:true
	}); 

	var addIndustryTypeLevelTwoStore=new Ext.data.Store({  
		reader:new Ext.data.JsonReader({ },[
		{name:'name'}, 
		{name:'code'}
		])
		}); 
	

var custTypeStore = new Ext.data.Store({ 
proxy:new Ext.data.HttpProxy({
url:'showType.do?type=CustType',
method:'GET'
}), 
reader:new Ext.data.JsonReader({ },[
{name:'name'}, 
{name:'code'}
]) 
});


var custScopeStore = new Ext.data.Store({ 
	proxy:new Ext.data.HttpProxy({
	url:'showType.do?type=CustScope',
	method:'GET'
	}), 
	reader:new Ext.data.JsonReader({ },[
	{name:'name'}, 
	{name:'code'}
	]) 
});



var exposureTypeStore = new Ext.data.Store({ 
	proxy:new Ext.data.HttpProxy({
	url:'showType.do?type=ExposureType',
	method:'GET'
	}), 
	reader:new Ext.data.JsonReader({ },[
	{name:'name'}, 
	{name:'code'}
	]) 
});
var addDebtTypeStore = new Ext.data.Store({ 
	proxy:new Ext.data.HttpProxy({
	url:'showType.do?type=DebtType',
	method:'GET'
	}), 
	reader:new Ext.data.JsonReader({ },[
	{name:'name'}, 
	{name:'code'}
	]) 
});
var classifyResultTypeStore = new Ext.data.Store({ 
	proxy:new Ext.data.HttpProxy({
	url:'showType.do?type=ClassifyResult',
	method:'GET'
	}), 
	reader:new Ext.data.JsonReader({ },[
	{name:'name'}, 
	{name:'code'}
	]) 
});

var addGuarantyKindStore = new Ext.data.Store({ 
	proxy:new Ext.data.HttpProxy({
	url:'showType.do?type=GuarantyKind',
	method:'GET'
	}), 
	reader:new Ext.data.JsonReader({ },[
	{name:'name'}, 
	{name:'code'}
	]) 
});

var grid=new Ext.grid.GridPanel({ 
bodyStyle: 'padding:5px',
id:'myCustomers',
renderTo:'result_panel',
stripeRows:true,//斑马线效果
title:"查询结果列表", 
height:420, 
cm:cm1,
store: ds, 
loadMask: { msg: "加载中..." }, 
renderer:function(value){
	return '<div align="center">'+value+'</div>';
},
viewConfig: {   
                            forceFit:true   
            },   
            bbar: new Ext.PagingToolbar({   
                pageSize: 20,   
                store: ds,   
                displayInfo: true,   
                displayMsg: '第{0} 到 {1} 条数据 共{2}条',   
                emptyMsg: "没有数据"   
            }) ,
            tbar:[
          		{text:'导出excel',handler:exportExcel},
          	]              
});


//查找客户表单
var form = new Ext.form.FormPanel({ 
renderTo:'search_panel',     
height : 270,
id: 'findForm', 
title: '债项查询',
anchor:'80%', 
bodyStyle: 'margin:3px;text-align:left;', 
frame: true, 
labelWidth: 100, 
items: 
[ {
	layout:"column",
	items:[{
		columnWidth : .5, // 该列有整行中所占百分比
	           layout : "form",
	           items : [{
	        	   xtype: 'textfield', 
	         	   id:'custName',
	         	   name: 'custName', 
	         	   fieldLabel: '客户名称',  
	         	   allowBlank: true, 
	         	   width:200   
	}]},{ columnWidth : .5, // 该列有整行中所占百分比
	           layout : "form",
	           items : [{
	}]} 
	]},

	{
	layout:"column",
	items:[{
		columnWidth : .5, // 该列有整行中所占百分比
	           layout : "form",
	           items : [{
	xtype: 'combo', 
	 id : "custType",
	 store :custTypeStore,
	fieldLabel: '客户类型',
	valueField : 'code',
	displayField : 'name',
	emptyText : '请选择客户类型...',
	allowBlank : true,
	typeAhead : true,
	width: 200,
	mode:'remote',  
	hiddenName:'custType',
	editable: false,
	triggerAction : 'all',
	selectOnFocus : true   
	}]},{ columnWidth : .5, // 该列有整行中所占百分比
	           layout : "form",
	           items : [{ 
	        	   xtype: 'combo',
					id: 'orgCode',
					//name: 'exposureType', 
					fieldLabel: '所属机构', 
					store:new Ext.data.SimpleStore({fields:['id', 'text'],data:[[]]}), 
					displayField: 'text',
					valueField: 'id',
					hiddenName: 'orgCode',
		            editable:false, 
		            mode: 'local', 
		            triggerAction:'all', 
		            frame:true,
		            border:true,
		            width:200,
		            tpl: '<div id="treePanel" style="height:180px;width:240px;"></div>', //html代码      
		            emptyText:'请选择客户所属机构...',
		            resizable:true
	         
	}]} 
	]},
	{layout:"column",
		items:[{
			columnWidth : .5, // 该列有整行中所占百分比
			layout : "form",
			items : [{
				xtype: 'combo', 
		 		id : "IndustryTypeLevelOne",
		       store :addIndustryTypeLevelOneStore,
		       fieldLabel: '行业大类',
		       valueField : 'code',
		       displayField : 'name',
		       typeAhead : true,
		       width: 200,
		       mode:'remote', 
		       emptyText : '请选择行业大类型...',
		       editable: false,
		       triggerAction : 'all',
		       selectOnFocus : true,
		       hiddenName:'industryTypeLevelOne' ,
		       listeners:{
		           select : function(combo, record,index)   
		           {
					var industryTypeLevelTwoCombo = Ext.getCmp('IndustryTypeLevelTwo');
					industryTypeLevelTwoCombo.clearValue(); //可以实现当下拉值变更时，清空之前下拉选项中的值   
					addIndustryTypeLevelTwoStore.proxy =new Ext.data.HttpProxy({
					url:'showIndustryTypeLevelTwo.do?LevelOneId='+combo.value,
					method:'GET'
					}); 
					addIndustryTypeLevelTwoStore.load(); //加载下拉框的store      
		           }      
		       }
	}]},{
			columnWidth : .5, // 该列有整行中所占百分比
			layout : "form",
		    items : [{
		    	xtype: 'combo', 
		 		id : "IndustryTypeLevelTwo",
		       store :addIndustryTypeLevelTwoStore,
		       fieldLabel: '行业子类',
		       valueField : 'code',
		       displayField : 'name',
		       typeAhead : true,
		       width: 200,
		       mode:'remote', 
		       emptyText : '请选择行业子类型...',
		       editable: false,
		       triggerAction : 'all',
		       selectOnFocus : true,
		       hiddenName:'industryTypeLevelTwo'        
		           
		}
		]} 
		]},
	{
		layout:"column",
	items:[{
		columnWidth : .5, // 该列有整行中所占百分比
	           layout : "form",
	           items : [{
	xtype: 'combo', 
	 id : "custScope",
	 store :custScopeStore,
	fieldLabel: '客户规模',
	valueField : 'code',
	displayField : 'name',
	emptyText : '请选择客户规模...',
	allowBlank : true,
	typeAhead : true,
	width: 200,
	mode:'remote',  
	hiddenName:'custScope',
	editable: false,
	triggerAction : 'all',
	selectOnFocus : true   
	}]},{ columnWidth : .5, // 该列有整行中所占百分比
	           layout : "form",
	           items : [{
	        	   xtype: 'combo', 
	        	   id : "exposureType",
	        	   store :exposureTypeStore,
	        	  fieldLabel: '风险暴露分类',
	        	  valueField : 'code',
	        	  displayField : 'name',
	        	  emptyText : '请选择风险暴露分类...',
	        	  allowBlank : true,
	        	  typeAhead : true,
	        	  width: 200,
	        	  mode:'remote',  
	        	  hiddenName:'exposureType',
	        	  editable: false,
	        	  triggerAction : 'all',
	        	  selectOnFocus : true   
	         
	}]} 
	]},
	
  {
	layout:"column",
items:[{
	columnWidth : .5, // 该列有整行中所占百分比
           layout : "form",
           items : [{
        	   xtype: 'textfield', 
        	   id:'debtId',
        	   name: 'debtId', 
        	   fieldLabel: '债项编号',  
        	   allowBlank: true, 
        	   width:200 
}]},{ columnWidth : .5, // 该列有整行中所占百分比
           layout : "form",
           items : [{
				xtype: 'textfield', 
				id:'debtName',
				name: 'debtName', 
				fieldLabel: '债项名称',  
				allowBlank: true, 
				width:200
         
}]} 
]},{
	layout:"column",
	items:[{
		columnWidth : .5, // 该列有整行中所占百分比
	           layout : "form",
	           items : [{
	        	   xtype: 'combo', 
	      		 id : "debtType",
	      		 store :addDebtTypeStore,
	      		fieldLabel: '债项品种',
	      		valueField : 'code',
	      		displayField : 'name',
	      		emptyText : '请选择债项品种...',
	      		allowBlank : true,
	      		typeAhead : true,
	      		width: 200,
	      		mode:'remote',  
	      		hiddenName:'debtType',
	      		editable: false,
	      		triggerAction : 'all',
	      		selectOnFocus : true   
	}]},{ columnWidth : .5, // 该列有整行中所占百分比
	           layout : "form",
	           items : [{
	        	   xtype: 'combo', 
	        		 id : "guarantyKind",
	        		 store :addGuarantyKindStore,
	        		fieldLabel: '担保类型',
	        		valueField : 'code',
	        		displayField : 'name',
	        		emptyText : '请选择担保类型...',
	        		allowBlank : true,
	        		typeAhead : true,
	        		width: 200,
	        		mode:'remote',  
	        		hiddenName:'guarantyKind',
	        		editable: false,
	        		triggerAction : 'all',
	        		selectOnFocus : true
	}]} 
	]},{
		layout:"column",
		items:[{
			columnWidth : .5, // 该列有整行中所占百分比
           layout : "form",
           items : [{
        	   xtype: 'combo', 
      		 id : "classifyResult",
      		 store :classifyResultTypeStore,
      		fieldLabel: '资产分类',
      		valueField : 'code',
      		displayField : 'name',
      		emptyText : '请选择资产分类...',
      		allowBlank : true,
      		typeAhead : true,
      		width: 200,
      		mode:'remote',  
      		hiddenName:'classifyResult',
      		editable: false,
      		triggerAction : 'all',
      		selectOnFocus : true   
		}]},{
			columnWidth : .5, // 该列有整行中所占百分比
		    layout : "form",
		    items : [{
		    	xtype: 'datefield',
				id: 'classifyDate', 
				name: 'classifyDate', 
				fieldLabel: '认定日期',
				allowBlank: true,
				editable:false,  
				format : 'Y-m' ,
				width: 200
		}
		]}
		]},

],
buttons:[{text:"查询", handler: findCustomer},
{text:"重置", handler: function(){ Ext.getCmp('findForm').form.reset();}}]
});

var tree = new Ext.tree.TreePanel
({ 
    animate:true,
    titleCollapse:true,
    root:root,
    singleExpand:false,
    iconCls:"icon-tree",
    autoLoad:false,
    allowDomMove:true,
    frame:true,
    collapsible:false,
    collapsed:false,
    collapseFirst:true,
    border:false,
    listeners:{ 
    	render:function(tree){ 
    		tree.el.on('click',function(e){ 
			e.stopPropagation(); }) ;
		} 
	} 
});

var loader=new Ext.tree.TreeLoader({dataUrl:'system/showOrgTree.do'});

var root=new Ext.tree.AsyncTreeNode
({
 id:"0", 
 leaf:false,
 loader: loader,
 text:"清远农商行总行",
 expandable:true,
 expanded:true 
});         

tree.setRootNode(root);

var comboTree =  Ext.getCmp('orgCode');      
var orgCode = "";
comboTree.on('expand',function(){tree.render('treePanel');});

var tree_click = function(node,e)
{
	 e.stopEvent();   //非叶子节点则不触发
     comboTree.setValue(node.id);  //设置option值
     comboTree.setRawValue(node.text);
     //comboTree.hiddenField.value = node.id;  
 	comboTree.collapse();   //隐藏option列表
// 	alert(comboTree.value);
// 	alert(comboTree.getValue());
// 	alert(comboTree.getRawValue());
// 	alert(comboTree.value);
 	orgCode = comboTree.value;
};
tree.on("click",tree_click);  

//查询客户 
function findCustomer() { 
	var findForm = Ext.getCmp("findForm");
	
	if(findForm.form.isValid()){
		
		findForm.form.doAction('submit', { 
                    url : 'RiskReport/findDebt.do?orgCode='+orgCode,
                    
                    method : 'post', 
                    success : function(form,action) { 
                    var resultCust = Ext.util.JSON.decode(action.response.responseText).result;
                    
                    if(JSON.stringify(resultCust)!='[]'){
                    ds.proxy = new Ext.data.MemoryProxy(resultCust);
					ds.load({params:{start:0,limit:20}});
					}
					else{
					ds.proxy = new Ext.data.MemoryProxy(resultCust);
					ds.load();
					Ext.Msg.alert('提示', '当前债项不存在！');}
                    }, 
                        failure : function() { 
						Ext.Msg.alert('提示', '请求异常，请重新查询！'); 
					} 
                    }); 
}
} ;
function exportExcel(){
	window.location.href='RiskReport/exportExcel.do?id=03';
};

});